package lightswitch.server.code.sqlhelper.expression.operators.relational;

import java.util.List;

import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class LikeExpression extends BinaryExpression {
	public LikeExpression(Expression left, Expression right, boolean isNot)
	{
			this.leftExpression = left;
			this.rightExpression=right;
			this.isNot=isNot;
			
	
	}
	public LikeExpression(String left, String right, boolean isNot, From _from)
	{
		leftExpression = ExpressionHelper.parseExpression(left,_from);
		rightExpression = ExpressionHelper.parseExpression(right,_from);
			
			this.isNot=isNot;
			
	
	}
	public String toString()
	{
		return "("+leftExpression.toString() + " LIKE " + rightExpression.toString() +" )";
	}
	public Object GetValue(DataRow row, List<From> parentFromList)
	{
		boolean res = false;
		dataType = TypeOfData.BOOLEAN;
		
		String m  = rightExpression.GetValue(row, null).toString();
		m = m.replaceAll("_",".");
		m = m.replace("%",".*");
		
		if(leftExpression.GetValue(row, null).toString().matches(m))
		{
			res=true;
		}
		else
			res=false;
		return isNot==true?!res:res;

	}

}
